﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:WebSiteConfig
	/// </summary>
	public partial class WebSiteConfig
	{
		public WebSiteConfig()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ConfigId", "WebSiteConfig"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ConfigId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from WebSiteConfig");
			strSql.Append(" where ConfigId=@ConfigId");
			SqlParameter[] parameters = {
					new SqlParameter("@ConfigId", SqlDbType.Int,4)
			};
			parameters[0].Value = ConfigId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.WebSiteConfig model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into WebSiteConfig(");
			strSql.Append("SiteName,Description,Copyright,KeyWords,Register,Email,Phone,ext1,ext2)");
			strSql.Append(" values (");
			strSql.Append("@SiteName,@Description,@Copyright,@KeyWords,@Register,@Email,@Phone,@ext1,@ext2)");
			strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@SiteName", SqlDbType.NVarChar,100),
					new SqlParameter("@Description", SqlDbType.NVarChar,200),
					new SqlParameter("@Copyright", SqlDbType.NVarChar,100),
					new SqlParameter("@KeyWords", SqlDbType.NVarChar,500),
					new SqlParameter("@Register", SqlDbType.NVarChar,100),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@ext1", SqlDbType.NVarChar,50),
                    new SqlParameter("@ext2",SqlDbType.NVarChar,50)};
			parameters[0].Value = model.SiteName;
			parameters[1].Value = model.Description;
			parameters[2].Value = model.Copyright;
			parameters[3].Value = model.KeyWords;
			parameters[4].Value = model.Register;
			parameters[5].Value = model.Email;
			parameters[6].Value = model.Phone;
			parameters[7].Value = model.ext1;
            parameters[8].Value = model.ext2;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.WebSiteConfig model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update WebSiteConfig set ");
			strSql.Append("SiteName=@SiteName,");
			strSql.Append("Description=@Description,");
			strSql.Append("Copyright=@Copyright,");
			strSql.Append("KeyWords=@KeyWords,");
			strSql.Append("Register=@Register,");
			strSql.Append("Email=@Email,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("ext1=@ext1,");
            strSql.Append("ext2=@ext2");
			strSql.Append(" where ConfigId=@ConfigId");
			SqlParameter[] parameters = {
					new SqlParameter("@SiteName", SqlDbType.NVarChar,100),
					new SqlParameter("@Description", SqlDbType.NVarChar,200),
					new SqlParameter("@Copyright", SqlDbType.NVarChar,100),
					new SqlParameter("@KeyWords", SqlDbType.NVarChar,500),
					new SqlParameter("@Register", SqlDbType.NVarChar,100),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@ext1", SqlDbType.NVarChar,50),
                    new SqlParameter("@ext2", SqlDbType.NVarChar,50),
					new SqlParameter("@ConfigId", SqlDbType.Int,4)};
			parameters[0].Value = model.SiteName;
			parameters[1].Value = model.Description;
			parameters[2].Value = model.Copyright;
			parameters[3].Value = model.KeyWords;
			parameters[4].Value = model.Register;
			parameters[5].Value = model.Email;
			parameters[6].Value = model.Phone;
			parameters[7].Value = model.ext1;
            parameters[8].Value = model.ext2;
			parameters[9].Value = model.ConfigId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ConfigId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from WebSiteConfig ");
			strSql.Append(" where ConfigId=@ConfigId");
			SqlParameter[] parameters = {
					new SqlParameter("@ConfigId", SqlDbType.Int,4)
			};
			parameters[0].Value = ConfigId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ConfigIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from WebSiteConfig ");
			strSql.Append(" where ConfigId in ("+ConfigIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.WebSiteConfig GetModel(int ConfigId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ConfigId,SiteName,Description,Copyright,KeyWords,Register,Email,Phone,ext1,ext2 from WebSiteConfig ");
			strSql.Append(" where ConfigId=@ConfigId");
			SqlParameter[] parameters = {
					new SqlParameter("@ConfigId", SqlDbType.Int,4)
			};
			parameters[0].Value = ConfigId;

			Maticsoft.Model.WebSiteConfig model=new Maticsoft.Model.WebSiteConfig();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ConfigId"]!=null && ds.Tables[0].Rows[0]["ConfigId"].ToString()!="")
				{
					model.ConfigId=int.Parse(ds.Tables[0].Rows[0]["ConfigId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["SiteName"]!=null && ds.Tables[0].Rows[0]["SiteName"].ToString()!="")
				{
					model.SiteName=ds.Tables[0].Rows[0]["SiteName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Description"]!=null && ds.Tables[0].Rows[0]["Description"].ToString()!="")
				{
					model.Description=ds.Tables[0].Rows[0]["Description"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Copyright"]!=null && ds.Tables[0].Rows[0]["Copyright"].ToString()!="")
				{
					model.Copyright=ds.Tables[0].Rows[0]["Copyright"].ToString();
				}
				if(ds.Tables[0].Rows[0]["KeyWords"]!=null && ds.Tables[0].Rows[0]["KeyWords"].ToString()!="")
				{
					model.KeyWords=ds.Tables[0].Rows[0]["KeyWords"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Register"]!=null && ds.Tables[0].Rows[0]["Register"].ToString()!="")
				{
					model.Register=ds.Tables[0].Rows[0]["Register"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Phone"]!=null && ds.Tables[0].Rows[0]["Phone"].ToString()!="")
				{
					model.Phone=ds.Tables[0].Rows[0]["Phone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ext1"]!=null && ds.Tables[0].Rows[0]["ext1"].ToString()!="")
				{
					model.ext1=ds.Tables[0].Rows[0]["ext1"].ToString();
				}
                if (ds.Tables[0].Rows[0]["ext2"] != null && ds.Tables[0].Rows[0]["ext2"].ToString() != "")
                {
                    model.ext2 = ds.Tables[0].Rows[0]["ext2"].ToString();
                }
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ConfigId,SiteName,Description,Copyright,KeyWords,Register,Email,Phone,ext1,ext2 ");
			strSql.Append(" FROM WebSiteConfig ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ConfigId,SiteName,Description,Copyright,KeyWords,Register,Email,Phone,ext1,ext2 ");
			strSql.Append(" FROM WebSiteConfig ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM WebSiteConfig ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ConfigId desc");
			}
			strSql.Append(")AS Row, T.*  from WebSiteConfig T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "WebSiteConfig";
			parameters[1].Value = "ConfigId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

